package com.kcnet.qk.controller.hdyh;

import android.app.Notification;
import android.app.PendingIntent;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/7/9.
 * 自动回复
 */

public class HdyhAoduReplyCTR extends BasePeopleController {



    // 由通知消息得到的姓名和内容
    private String name,scontent;
    // 我们要回复的消息
    private String mcontent;


    /**
     * 存放关键词的集合
     */
    private List mKeyWordList = new ArrayList();
    /**
     * 存放回复内容的集合
     */
    private List mContentList = new ArrayList();


    private static HdyhAoduReplyCTR mRongCloudInstance;


    protected static final String TAG = "自动回复";

    public static HdyhAoduReplyCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (HdyhAoduReplyCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new HdyhAoduReplyCTR();
                }
            }
        }

        return mRongCloudInstance;
    }



    @Override
    public void doTask(AccessibilityEvent event) throws Exception {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();
        switch (mType) {
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:// 通知栏事件
                NLogger.d(TAG,"1.监听到通知栏有微信消息");
                List<CharSequence> texts = event.getText();
                if (!texts.isEmpty()) {
                    for (CharSequence text : texts) {
                        String content = text.toString();
                        if (!TextUtils.isEmpty(content)) {
                            NLogger.d(TAG,"2.点击通知栏 进入聊天界面");
                                sendNotifacationReply(event);
                        }
                    }
                    }
                    break;
                    case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED://微信界面事件
                        NLogger.d(TAG,"3.聊天界面 发送自动回复话语");
                        String className = event.getClassName().toString();
                        if (className.equals("com.tencent.mm.ui.LauncherUI")) {
                                sendText();
                        }
                        NLogger.d(TAG,"5.返回到桌面首页");
                        RootShell.keyevent(3);
                        break;

                }

    }


    /**
     * @param event
     */
    private void sendNotifacationReply(AccessibilityEvent event) {
        if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
            Notification notification = (Notification) event.getParcelableData();
            String content = notification.tickerText.toString();
            String[] cc = content.split(":");
            // 获取通知栏中消息
            name = cc[0].trim();
            scontent = cc[1].trim();
            PendingIntent pendingIntent = notification.contentIntent;
            try {
                sleepTime(1000);
                pendingIntent.send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
    }



    /**
     * 根据关键词回复内容

     */
    public String contentResponse() {
        mcontent = null;
        for (int i = 0; i < mKeyWordList.size(); i++) {
            if (mKeyWordList.get(i) != "") {
                if (!(scontent.indexOf(mKeyWordList.get(i).toString().trim(), 0) == -1)) {
                    return mcontent = mContentList.get(i).toString();
                }
            }
        }
        return mcontent = "正在忙，稍后回复您，么么哒";
    }



    /**
     * 传进要回复的文字，复制到发送框里面，等待发送按钮的确认
     *
     * @param
     * @return
     */
    private void sendText() {

        RootShell.click(90,900);
        sleepTime(1000);
        inputSayHiSentence(contentResponse());
        sleepTime(1000);
        RootShell.click(465,900);
        sleepTime(1000);
        NLogger.d(TAG,"4.发送成功");
    }

    /**
     *

     */
    public void setParms(int status,List<String> keyList,List<String> conteList){
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        mKeyWordList = keyList;
        mContentList = conteList;

        if(status == 0){
            stopTask();
        }
    }


}
